<template>
  <div class="proStirageOrd">
    <div style="margin-bottom: 10px">
      <jc-button
        :btn-arr="['save', 'remove', 'pass', 'sub', 'reload', 'option', 'list', !list.length && 'check']"
        :disab-arr="[isDisabled && 'save', 'close', 'cancel', 'com1', 'toExcel', 'com2', 'using', 'freeze']"
        :fbillno="production.fbillNo"
        :list="'/proTuiStock'"
        @refresh="getSCLLinfo"
        @save="proSave"
        @submit="proSave(1)"
        @noSubmit="submit(2)"
        @audit="audit(1)"
        @noAudit="audit(2)"
        @del="delStirage"
        @check="check('up')"
        @check1="check('down')"
      />
    </div>
    <el-tabs v-model="activeName" @tab-click="handleOther">
      <el-tab-pane :label="$t('market.info')" name="first">
        <el-form ref="purchaseRef" :model="production" label-width="4.68vw">
          <el-form-item :label="$t('market.documentstatus')">
            <!--            <el-input v-model.trim="production.statusName" disabled />-->
            <el-tag :type="production.stateColor">
              {{ production.fdocumentstatus }}
            </el-tag>
          </el-form-item>
          <el-form-item :label="$t('market.rkdh')">
            <el-input
              v-model.trim="production.fbillNo"
              disabled
            />
          </el-form-item>
          <el-form-item :label="$t('market.stocker')">
            <el-input v-model.trim="production.fstockerName" :disabled="isDisabled">
              <i
                slot="suffix"
                class="el-input__icon el-icon-search"
                @click="openStocker=true"
              />
            </el-input>
          </el-form-item>
          <el-form-item :label="$t('market.fdate')">
            <el-date-picker
              v-model="production.fdate"
              type="date"
              value-format="yyyy-MM-dd"
              size="mini"
              :disabled="isDisabled"
            />
          </el-form-item>
          <!--          <el-form-item label="生产组织">-->
          <!--            <el-input v-model.trim="production.fprdorg" disabled />-->
          <!--          </el-form-item>-->
          <el-form-item :label="$t('market.note')">
            <el-input v-model.trim="production.fremark" :disabled="isDisabled" />
          </el-form-item>
        </el-form>
        <div style="margin-top: 10px">
          <div>
            <el-button size="mini" disabled>{{ $t('btn.newHang') }}<!--新增行--></el-button>
            <el-button size="mini" style="margin-bottom: 5px;" @click="delHang">{{ $t('btn.delHang') }}<!--删除行--></el-button>
          </div>
          <jc-table
            :table-data="tableData"
            :table-header="tableHeard"
            :table-height="tableHeight"
            :get-summaries="getSummaries"
            show-summary
            @rowClick="rowClick"
          >
            <!--物料编码-->
            <template v-slot:orderSlot="col">
              <el-link type="primary" @click="sonJumpMateriel(col.scope.row.fnumber)">
                {{ col.scope.row.fnumber }}
              </el-link>
            </template>
            <!--实发数量-->
            <template v-slot:btnSlot="col">
              <el-input-number
                v-model="col.scope.row.frealQty"
                :disabled="isDisabled"
                :min="0"
                :max="Number(col.scope.row.sqsl)"
                size="mini"
              />
            </template>
            <!--库存数量-->
            <template v-slot:tagSlot="col">
              <span>{{ col.scope.row.kCqty || 0 }}</span>
            </template>
            <!-- 仓库-->
            <template v-slot:billSlot="col">
              <el-input
                v-model="col.scope.row.fstockName"
                size="mini"
                :disabled="isDisabled"
              >
                <i
                  slot="suffix"
                  class="el-input__icon el-icon-search"
                  @click="handleWarehouse(col.scope.$index)"
                />
              </el-input>
            </template>
          </jc-table>
        </div>
      </el-tab-pane>
      <el-tab-pane :label="$t('market.other')" name="log">
        <jc-marker
          ref="log"
          :fnumber="production.fbillNo"
          other-height="68vh"
          :other-url-object="otherUrlObject"
        />
      </el-tab-pane>
    </el-tabs>
    <!--    仓管员-->
    <stocker v-if="openStocker" @stocker="stockerData" />
    <selectCK v-if="seleceVisiblit" :fmaterial-id="stockFnumber" @client="selectData" />
    <upDown v-if="upDownVis" :msg="cha" :fentryid="fentryData" @client="upDownVis=false" />
  </div>
</template>

<script>
import {
  queryUninstockDetails,
  updatePrdUninstockDetails,
  delPrdUninstocks, submitPrdUninstock,
  notsubmitPrdUninstock, auditPrdUninstocks, unauditPrdUninstocks
} from '@/api/production/productionList'
import operability from '@/mixin/Operability'
import jcButton from '@/components/Button'
import { toMxBmina } from '@/components/ToMxamineState'
import jcMarker from '@/components/marker'
import jumpMateriel from '@/components/JumpMateriel'

export default {
  name: 'EditMaterial',
  components: {
    stocker: () => import('@/views/purchasing/putStorage/editStorage/component/stocker'),
    upDown: () => import('@/components/upDown'),
    selectCK: () => import('@/views/production/collect/components/seleceCK'),
    jcButton,
    jcMarker
  },
  mixins: [jumpMateriel, operability],
  props: {
    list: {
      type: Array,
      default: function() {
        return []
      }
    }
  },
  data() {
    return {
      upDownVis: false,
      openStocker: false,
      cha: '',
      fentryData: [],
      targerName: 'proStirageOrder',
      activeName: 'first',
      production: {},
      isDisabled: false,
      seleceVisiblit: false, // 仓库
      indexSelf: 0,
      stockFnumber: '',
      otherUrlObject: {},
      tableAudit: [],
      tableHeight: '74vh',
      tableData: [],
      tableHeard: [
        { label: this.$t('market.fseq'), prop: 'fseq', width: '80' },
        { label: this.$t('market.fmaterial'), prop: 'fnumber', type: 'order', minWidth: '120px' },
        { label: this.$t('market.fdescripTion'), prop: 'fdescripTion', minWidth: '320px' },
        { label: this.$t('market.fmodel'), prop: 'fmodel', minWidth: '100px' },
        { label: this.$t('market.specificaTion'), prop: 'fspecificaTion', minWidth: '120px' },
        { label: this.$t('market.funit'), prop: 'funitName', width: '80' },
        { label: this.$t('market.appQty'), prop: 'fmustQty', width: '80' },
        { label: this.$t('market.realqty2'), type: 'btn', prop: 'frealQty', width: '80px' },
        { label: this.$t('market.stock2'), type: 'bill', prop: 'fstockName', width: '160px' },
        { label: this.$t('market.stockQty'), prop: 'stockQty', width: '80' },
        { label: this.$t('market.salKhddh'), prop: 'salKhddh', minWidth: '100px' },
        { label: this.$t('market.deaprt'), prop: 'fworkshop', minWidth: '100px' },
        { label: this.$t('market.mobillNo'), prop: 'fmobillNo' },
        { label: this.$t('market.moentrySeq'), prop: 'fmoentrySeq', width: '80' }
        // { label: '入库组织', prop: 'fstockorg' },
        // { label: '货主', prop: 'fowner' }
      ],
      delArr: []
    }
  },
  created() {
    if (this.$route.params.id) {
      this.getSCLLinfo()
    }
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 270px)' : '72vh'
  },
  methods: {
    // 入库单数据
    getSCLLinfo() {
      const DATA = {
        fid: this.$route.params.id
      }
      queryUninstockDetails(DATA).then(res => {
        if (res.code === 0) {
          this.production = toMxBmina(res.data)
          this.otherUrlObject = res.data
          this.tableData = res.data.details
          this.isDisabled = this.production.fdocumentstatus === this.$t('market.state1') || this.production.fdocumentstatus === this.$t('market.state2')
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 保存
    proSave(ev) {
      this.detailList = []
      this.tableData.forEach(item => {
        this.detailList.push({
          funitId: item.funitId,
          fstockId: item.fstockId,
          fentryId: item.fentryId,
          frealQty: item.frealQty
        })
      })
      const { fid, fdate, fremark, fstockerId } = this.production
      const DATA = {
        fid,
        fdate,
        fremark, fstockerId,
        delArr: this.delArr,
        details: this.detailList
      }
      updatePrdUninstockDetails(DATA).then(res => {
        if (res.code === 0) {
          ev === 1 ? this.submit(1) : this.getSCLLinfo()
          this.$message({ showClose: true, message: res.message, type: 'success' })
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 公共函数
    toExamineFun(api) {
      this.fentryData = []
      this.tableData.forEach(res => {
        this.fentryData.push(res.fentryId)
      })
      const DATA = {
        Numbers: [this.production.fbillNo],
        fIds: [this.production.fid],
        EntryIds: this.fentryData
      }
      api(DATA).then(res => {
        if (res.code !== 0) {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
          return
        }
        this.getSCLLinfo()
        this.innerHTM = ''
        res.data.forEach(res => {
          this.innerHTM += '<div>' + res + '</div>'
        })
        this.$notify.warning({
          title: '提示',
          message: this.innerHTM,
          duration: 3000, offset: 50,
          dangerouslyUseHTMLString: true
        })
      })
    },
    // 提交
    async submit(ev) {
      const api = ev === 1 ? submitPrdUninstock : notsubmitPrdUninstock
      this.toExamineFun(api)
    },
    // 审核
    async audit(ev) {
      const api = ev === 1 ? auditPrdUninstocks : unauditPrdUninstocks
      this.toExamineFun(api)
    },
    // 删除
    delStirage() {
      if (this.isDisabled) {
        this.$confirm('请先反审核后再删除', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        })
        return
      }
      this.$confirm('确定要删除该订单么?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const DATA = { Numbers: [this.production.fbillNo] }
        delPrdUninstocks(DATA).then(res => {
          if (res.code !== 0) {
            this.$confirm(res.message, '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'error'
            })
            return
          }
          // 关闭当前页面
          this.$store.dispatch('tagsView/guanbi', this.$route)
          // 跳转到修改页面
          this.$router.push({ path: '/proTuiStock' })
        })
      })
    },
    // 打开仓库弹窗
    handleWarehouse(index) {
      this.indexSelf = index
      this.stockFnumber = this.tableData[index].fnumber
      this.seleceVisiblit = true
    },
    // 获取仓库数据
    selectData(ev) {
      if (ev.fstockId) {
        const item = this.tableData[this.indexSelf]
        item.fstockId = ev.fstockId
        item.fstockName = ev.fname
        item.stockQty = ev.sumQty
        item.cKfnumber = ev.fnumber
      }
      this.seleceVisiblit = false
    },
    // 仓管员
    stockerData(item) {
      if (item.fuserId) {
        this.production.fstockerId = item.fuserId
        this.production.fstockerName = item.fuserName
      }
      this.openStocker = false
    },
    // 上查、下查
    check(ev) {
      this.fentryData = []
      this.tableData.forEach(res => {
        this.fentryData.push(res.fentryId)
      })
      this.cha = ev
      this.upDownVis = true
    },
    // 单击行
    rowClick(row) {
      this.indexSelf = row.index
    },
    // 删除行
    delHang() {
      const id = this.tableData[this.indexSelf].fentryId
      id && this.delArr.push(id)
      this.tableData.splice(this.indexSelf, 1)
    },
    // 操作记录
    handleOther(tab) {
      if (tab.name === 'log') {
        this.$refs.log.otherlog()
      }
    }
  }
}
</script>

<style lang="scss" scope>
.proStirageOrd {
  @include listBom;
  .el-tabs {
    .el-form {
      display: flex;
      flex-wrap: wrap;
      .el-input .el-input__inner {
        height: 30px;
        line-height: 30px;
      }
      .el-form-item {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        height: 35px;
        width: 275px;
        margin-bottom: 0.46vh;
        .btnType {
          margin-right: 5px;
        }

        .el-date-editor.el-input {
          width: 100% !important;
        }

        .el-select {
          width: 100%;
        }
      }
    }
  }
}
</style>
